<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          placeholder="请输入商品名称"
          v-model="queryParams.goodsName"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"/>
        <el-button style="margin-left:15px" type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索
        </el-button>
      </el-form-item>
      <el-form-item><el-button style="background:rgb(238, 17, 17);color:white" size="mini" @click="removeGoods">移除所有已失效商品</el-button></el-form-item>
    </el-form>
    

    <el-table v-loading="loading" :data="cartList" tooltip-effect="light">
      <el-table-column label="商品主图" prop="goodsImg" width="220px" align="center">
        <template slot-scope="scope">
          <el-popover
            placement="right"
            width=atuo
            trigger="hover">
            <img :src="scope.row.goodsImg" style="height:200px;width:200px"/>
            <img slot="reference" :src="scope.row.goodsImg" style="max-height:100px;max-width:100px">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" prop="goodsName" width="150px" align="center" :show-overflow-tooltip="true"/>
      <el-table-column label="参数说明" prop="goodsSku" width="300px" align="center" :show-overflow-tooltip="true"  />
      <el-table-column label="价格" prop="goodsPrice" width="150px" align="center">
        <template slot-scope="scope">
          <el-tag type="danger">{{ scope.row.goodsPrice / '100' }}元</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="折扣" prop="goodsDiscount" width="125px" align="center">
        <template slot-scope="scope">
          <el-tag type="warning">{{ scope.row.goodsDiscount == '100' ? '无折扣' : scope.row.goodsDiscount / '10' + '折' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="库存" prop="goodsStock" width="125px" align="center">
        <template slot-scope="scope">
          <el-tag style="background:#CDF5FF;color:#1C87B5">{{ scope.row.goodsStock }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200px" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            style="background:#23BB83;color:white"
            icon="el-icon-shopping-cart-full"
            @click="goTo(scope.row.goodsId)"
          >购买
          </el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="delGoods(scope.row.carId)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listCart, addCart, delCart, deleCart } from '@/api/shop/shoppingcart'

export default {
  name: 'Shoppingcart',
  inject: ['reload'],//注入刷新当前页面的依赖
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 购物车表格数据
      cartList: null,
      // 表单参数
      form: {},
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsName: undefined
      }
    }
  },
  created() {
    console.log('创建表单')
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      listCart(this.addDateRange(this.queryParams, this.dateRange)).then(
        response => {
          this.cartList = response.rows
          this.total = response.total
          this.loading = false
        }
      )
    },
    // 表单重置
    reset() {
      this.form = {
        goodsId: undefined,
        goodsName: undefined,
        goodsClassify: undefined,
        goodsPrice: undefined,
        goodsDiscount: undefined,
        goodsStock: undefined,
        mainImgUrl: undefined,
        goodsDescribe: undefined,
        goodsSku: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    removeGoods() {
      this.reload()
    },
    delGoods(carId) {
      delCart(carId).then(res => {
        if (res.code === 200) {
          this.msgSuccess('删除成功')
          this.queryParams.pageNum = 1
          this.getList()
        }
      })
    },
    goTo(goodsId) {
      this.$router.push({ path: '/orderdetail', query: { goods_id: goodsId } })
    }
  }
}
</script>

<style scoped>
.el-tooltip__popper {
  font-size: 14px; 
  width:300px;
}
</style>
